package com.geekcattle.controller.map;

import com.alibaba.fastjson.JSON;
import com.geekcattle.service.baiDuMap.BaiDuMapService;
import com.geekcattle.util.MapUtil;
import com.geekcattle.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @program: geek-framework
 * @description:
 * @author: LQ
 * @create: 2019-05-09 00:09
 */
@RestController
@RequestMapping("/baiDuMap")
public class BaiDuMapController {

    @Autowired
    private BaiDuMapService baiDuMapService;

    @RequestMapping(value = "/savemap", method = {RequestMethod.GET, RequestMethod.POST})
    @Transactional
    public Map savemap(@RequestParam Map map) {
        System.out.println("map:" + map);
        List<Map> listMap = (List<Map>) JSON.parse(map.get("data") + "");
        Iterator<Map> iterator = listMap.iterator();
        try {
            while (iterator.hasNext()) {
                Map m = iterator.next();
                System.out.println(m);
                m.put("id", UuidUtil.getUUID());
                m.put("dots", m.get("dots") + "");
                baiDuMapService.saveMap(m);
            }
            return MapUtil.infoMap(1, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return MapUtil.infoMap(0, "保存失败");
        }
    }

    @RequestMapping(value = "/getmap", method = {RequestMethod.GET, RequestMethod.POST})
    @Transactional
    public Map getmap(@RequestParam Map map) {
        System.out.println("map:" + map);
        List<Map> listMap = baiDuMapService.getmap(map);
        Iterator<Map> iterator = listMap.iterator();
        while (iterator.hasNext()) {
            Map next = iterator.next();
            List<Map> list = (List<Map>) JSON.parse(next.get("dots") + "");
            next.put("dots", list);
        }
        return MapUtil.resultMap(listMap);
    }
    @RequestMapping(value = "/modifyMap", method = {RequestMethod.GET, RequestMethod.POST})
    @Transactional
    public Map modifyMap(@RequestParam Map map) {
        System.out.println("map:"+map);
        int result = baiDuMapService.modifyMap(map);
        if (result >0){
            return MapUtil.infoMap(1,"修改成功");
        }else {
            return MapUtil.infoMap(0,"修改失败");
        }
    }
    @RequestMapping(value = "/deleteMap", method = {RequestMethod.GET, RequestMethod.POST})
    @Transactional
    public Map deleteMap(@RequestParam Map map) {
        System.out.println("map:"+map);
        int result = baiDuMapService.deleteMap(map);
        if (result >0){
            return MapUtil.infoMap(1,"删除成功");
        }else {
            return MapUtil.infoMap(0,"删除失败");
        }
    }
}
